<?php

class UserController extends HomeController {

         public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

   	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}
	public function actionLogin() {
        
        $model = new LoginForm;

        // if it is ajax validation request
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }

        // collect user input data
        if (isset($_POST['LoginForm'])) {
            $model->attributes = $_POST['LoginForm'];
            // validate user input and redirect to the previous page if valid
            if ($model->validate() && $model->login())
                $this->redirect(Yii::app()->user->returnUrl);
        }
        // display the login form
        $this->render('login', array('model' => $model));
    }         
	private function lastViset() {
		//$lastVisit = FrontendUsersAccountModel::model()->notsafe()->findByPk(Yii::app()->user->id);
		//$lastVisit->lastvisit = time();
		//$lastVisit->save();
	}
	public function actionProfile()
	{		
		$id = Yii::app()->user->id;
		$model = User::model()->findByPk($id);
		$this->render('profile', array(
					'model'=>$model
				));
	}
    	public function actionRegistration() {
            $model = new RegistrationForm;
            $profile=new User;
            
            
			// ajax validator
			if(isset($_POST['ajax']) && $_POST['ajax']==='registration-form')
			{
				echo UActiveForm::validate(array($model,$profile));
				Yii::app()->end();
			}
			
		    if (Yii::app()->user->id) {
		    	$this->redirect(Yii::app()->controller->module->profileUrl);
		    } else {
		    	if(isset($_POST['RegistrationForm'])) {
					$model->attributes=$_POST['RegistrationForm'];					
					if($model->validate())
					{        
                                            $model->status=1;						
                                            if ($model->save()) {                                                    
                                            RegistrationForm::sendMail($model->email,
                                                    "You",
                                                    " Thank you for your registration. ");
                                            Yii::app()->user->setFlash('registration',("Thank you for your registration. Please check your email."));
                                            $this->refresh();
                                            }					
					}                                         
				}
			    $this->render('/user/registration',array('model'=>$model,'profile'=>$profile));
		    }
	}


        }

